﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO.Ports;
using JoystickInterface;
using DirectX.Capture;

namespace ROVController
{
    public partial class MainForm : Form
    {
        private Joystick jst = null;
        private Capture capture1 = null;
        private Capture capture2 = null;
        private Filters filters;

        private ROV robot;

        private bool jstConn = false;

        public MainForm()
        {
            InitializeComponent();
            InitPeripherals();
        }

        public void InitPeripherals()
        {
            //Aquire Robot via Serial Connection
            SerialPort serialPort = new SerialPort(Properties.Settings.Default.SerialPort);
            robot = new ROV(serialPort);
            

            //Setup the joystick
            jst = new JoystickInterface.Joystick(this.Handle);
            string[] sticks = jst.FindJoysticks();
            if (sticks != null)
            {
                jst.AcquireJoystick(sticks[0]);
                tmrUpdateStick.Interval = 100;
                jstConn = true;
                rovhud1.Joystick = ConrtolLibrary.StatusLightMode.OK;
            }
            else
            {
                //No joysticks found, setup jskcheck
                tmrUpdateStick.Interval = 2000;
                jstConn = false;
                rovhud1.Joystick = ConrtolLibrary.StatusLightMode.OFFLINE;
            }

            tmrUpdateStick.Enabled = true;

            //Grab Video Devices
            try
            {
                filters = new Filters();

                //Initialize Cameras
                if (filters.VideoInputDevices.Count > 0)
                {
                    capture1 = new Capture(filters.VideoInputDevices[0], filters.AudioInputDevices[0]);
                }
                if (filters.VideoInputDevices.Count > 1)
                {
                    capture2 = new Capture(filters.VideoInputDevices[1], filters.AudioInputDevices[1]);
                }
            }
            catch
            {
                //no devices found
            }            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //setup video(s)
            if(capture1 != null)
            {
                capture1.FrameSize = new Size(640, 480);
                try
                {
                    if (capture1.PreviewWindow == null)
                        capture1.PreviewWindow = panelVideo1;
                    else
                        capture1.PreviewWindow = null;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to enable/disable preview. Please submit a bug report.\n\n" + ex.Message + "\n\n" + ex.ToString());
                }
            }

            if (capture2 != null)
            {
                capture2.FrameSize = new Size(640, 480);
                try
                {
                    if (capture2.PreviewWindow == null)
                        capture2.PreviewWindow = panelVideo1;
                    else
                        capture2.PreviewWindow = null;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to enable/disable preview. Please submit a bug report.\n\n" + ex.Message + "\n\n" + ex.ToString());
                }
            }
        }

        #region Joystick Functions

        private void tmrUpdateStick_Tick(object sender, EventArgs e)
        {
            //update joystick values
            if (jstConn)
            {
                try
                {
                    //jsk update code here
                }
                catch
                {
                    jstConn = false;
                }
            }
            else
            {
                //check for joysticks
                string[] sticks = jst.FindJoysticks();
                if (sticks != null)
                {
                    jst.AcquireJoystick(sticks[0]);
                    tmrUpdateStick.Interval = 100;
                    jstConn = true;
                    rovhud1.Joystick = ConrtolLibrary.StatusLightMode.OK;
                }
                else
                {
                    tmrUpdateStick.Interval = 2000;
                    jstConn = false;
                    rovhud1.Joystick = ConrtolLibrary.StatusLightMode.OFFLINE;
                }
            }
        }
        
        #endregion

        #region Menu Functions

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutUROV dialog = new AboutUROV();
            dialog.Show();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Settings dialog = new Settings();
            dialog.Show();
        }

        #endregion

        
        
    }
}
